﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            SetInitialRow();
            ViewState["index"] = 0;
            ViewState["total"] = 0;
            ViewState["bv"] = 0;
            ViewState["pv"] = 0;
           setprice(0);
        }



    }

    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("DropDownList3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dt.Columns.Add(new DataColumn("Column5", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["DropDownList3"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dr["Column4"] = string.Empty;
        dr["Column5"] = string.Empty;
        dt.Rows.Add(dr);
        //dr = dt.NewRow();

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }//initial table

    private void AddNewRowToGrid()//add new row to table
    {
      int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                   
                    
                    dtCurrentTable.Rows[i - 1]["DropDownList3"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                    dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
                    dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;
                   
                    rowIndex++;
                   
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                ViewState["index"] = dtCurrentTable.Rows.Count - 1;
                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }



    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");


                    box1.Text = dt.Rows[i]["DropDownList3"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();
                    box4.Text = dt.Rows[i]["Column4"].ToString();
                    box5.Text = dt.Rows[i]["Column5"].ToString();

                    rowIndex++;
                }
            }
        }
    }

    protected void ASPxGridView1_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
    {
        if (e.Column.FieldName == "Total")
        {
            decimal price = Convert.ToDecimal(e.GetListSourceFieldValue("Pur_Price"));
            int qty = Convert.ToInt32(e.GetListSourceFieldValue("Qty"));
            decimal tot = price * qty;
            e.Value = tot;
        }
        if (e.Column.FieldName == "Pur_Price")
        {
            string pro_name = Convert.ToString(e.GetListSourceFieldValue("Pro_Name"));
            decimal price = Bestlife.ERP.BusinessRules.productBV_PV.productprice(pro_name);
            e.Value = price;
        }
    }

    protected void ButtonAdd_Click1(object sender, EventArgs e)
    {
        try
        {
            int rowIndex = 0;
            DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
            TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
            TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
            TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
            TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");

            if (box3.Text != null)
            {
                if (box4.Text != "")
                {
                    if (box5.Text != "")
                    {
                        AddNewRowToGrid();
                        
                    }
                    else
                    {
                        Label3.Visible = true;
                        Label3.Text = "You must enter a value for all required fields ";
                        setprice(0);

                    }

                }
                else
                {
                    Label3.Visible = true;
                    Label3.Text = "You must enter a value for all required fields ";
                    setprice(0);

                }
            }
            else
            {
                Label3.Visible = true;
                Label3.Text = "You must enter a value for all required fields ";
                setprice(0);

            }

        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//row adding

    protected void ButtonAdd_Click2(object sender, EventArgs e)
    {
        try
        {
            Gridview1.EditIndex = -1;
            Gridview1.DataBind();
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }


    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        // int index =Gridview1.SelectedIndex+1;  
        int index = int.Parse(ViewState["index"].ToString());
        setprice(index);

    }

    public void setprice(int index)
    {
        DropDownList pro = (DropDownList)Gridview1.Rows[index].FindControl("DropDownList3");
        string product = pro.Text;
        decimal price = Bestlife.ERP.BusinessRules.B_PurchaseOrder.getprice(product);
        TextBox box3 = (TextBox)Gridview1.Rows[index].Cells[3].FindControl("TextBox3");
        box3.Text = price.ToString();

    }

    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {

        try
        {
            int index = int.Parse(ViewState["index"].ToString());
            TextBox box3 = (TextBox)Gridview1.Rows[index].Cells[3].FindControl("TextBox3");
            TextBox box4 = (TextBox)Gridview1.Rows[index].Cells[4].FindControl("TextBox4");

            decimal qty;
            decimal price;
            try
            {
                qty = decimal.Parse(box4.Text);
                price = decimal.Parse(box3.Text);
                Label3.Visible = false;
                decimal tot = Bestlife.ERP.BusinessRules.B_PurchaseOrder.linetot(qty, price);

//total bv,pv count
                Bestlife.ERP.Entities.E_PurchaseOrder pro=new Bestlife.ERP.Entities.E_PurchaseOrder();
                DropDownList box1 = (DropDownList)Gridview1.Rows[index].Cells[1].FindControl("DropDownList3");
                pro.Proname=box1.Text;
                Bestlife.ERP.BusinessRules.B_PurchaseOrder.getbvpv(pro);
                 ViewState["bv"] =decimal.Parse(ViewState["bv"].ToString()) + tot;
                 ViewState["pv"] = decimal.Parse(ViewState["pv"].ToString()) + tot; 



                ViewState["total"] = decimal.Parse(ViewState["total"].ToString()) + tot;
                Label4.Visible = true;
                Label4.Text = ViewState["total"].ToString();
                TextBox box5 = (TextBox)Gridview1.Rows[index].Cells[5].FindControl("TextBox5");
                box5.Text = tot.ToString();
            }

            catch
            {
                qty = 0;
                price = 0;
                Label3.Visible = true;
                Label3.Text = "Invalid data type";

            }

        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }

    #region insert
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        string user = Session["username"].ToString();
        try
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList box1 = (DropDownList)Gridview1.Rows[i].Cells[1].FindControl("DropDownList3");
                    TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[i].Cells[5].FindControl("TextBox5");

                    string pro = box1.Text;
                    string description = box2.Text;
                    string unitvalue = box3.Text;
                    string qty = box4.Text;

                    Bestlife.ERP.Entities.E_PurchaseOrder order = new Bestlife.ERP.Entities.E_PurchaseOrder();
                   
                    order.Proname = pro;
                    order.Qty = decimal.Parse(qty);
                    order.Unitvalue = decimal.Parse(unitvalue);
                    order.Description = description;



                    Bestlife.ERP.BusinessRules.B_PurchaseOrder.insert(order, user);


                }

                Bestlife.ERP.Entities.E_PurchaseOrder order1 = new Bestlife.ERP.Entities.E_PurchaseOrder();

                //order1.Or_ID1 = ASPxTextBox1.Text;
                //order1.Contrat = DropDownList2.Text;
                //order1.Due = ASPxDateEdit2.Date;
                //order1.Issu = ASPxDateEdit1.Date;
                //order1.Supplier = DropDownList2.Text;

                //Bestlife.ERP.BusinessRules.B_PurchaseOrder.insertorder(order1);
                //Bestlife.ERP.BusinessRules.B_PurchaseOrder.insertsupp(order1, user);

                Bestlife.ERP.Entities.E_PurchaseOrder total = new Bestlife.ERP.Entities.E_PurchaseOrder();
                total.Total = decimal.Parse(Label4.Text);
                //total.Or_ID1 = ASPxTextBox1.Text;
                Boolean result = Bestlife.ERP.BusinessRules.B_PurchaseOrder.total(total, user);
                if (result)
                {
                    Label3.Text = "You have succesfully added your purchase order";
                    Label3.Visible = true;
                    //ASPxImage1.Visible = true;
                }

            }

        }
        catch
        {
            Label3.Text = "Purches order fail";
            Label3.Visible = true;
        }
    }
    #endregion

    protected void ASPxButton2_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("Cash.aspx");
        }
        catch
        {
            Response.Redirect("404.aspx");
        }
    }//cancle
}